package com.hison.aspect;

import com.alibaba.fastjson.JSON;
import com.hison.annotation.SystemLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: LogAspect
 * @Descripiton: TODO
 * @Author: 小郑
 * @Date： 2024/1/13 21:46
 * @Version：1.8
 **/
@Component
@Aspect
@Slf4j
public class LogAspect {
    @Pointcut("@annotation(com.hison.annotation.SystemLog)")
    public void pt() {

    }

    @Around("pt()")
    public Object printLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object ret = null;
        try {
            handleBefore(joinPoint);
            ret = joinPoint.proceed();
            handleAfter(ret);
        } finally {
            log.info("=====end======" + System.lineSeparator());

        }
        return ret;
    }

    private void handleAfter(Object  ret) {
        log.info("Response      : {}",JSON.toJSONString(ret));
    }

    private void handleBefore(ProceedingJoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //获取被增强方法上的注解对象
        SystemLog systemLog = getSystemLog(joinPoint);

        log.info("======================Start======================");
        // 打印请求 URL
        log.info("请求URL   : {}", request.getRequestURL());
// 打印描述信息
        log.info("接口描述   : {}", systemLog.businessName());
// 打印 Http method
        log.info("请求方式   : {}", request.getMethod());
// 打印调用 controller 的全路径以及执行方法
        log.info("请求类名   : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), ((MethodSignature) joinPoint.getSignature()).getName());
// 打印请求的 IP
        log.info("访问IP    : {}", request.getRemoteHost());
// 打印请求入参
        log.info("传入参数   : {}", JSON.toJSONString(joinPoint.getArgs()));
// 打印出参
        log.info("返回参数   : {}", "");

// 结束后换行
        log.info("=======================end=======================" + System.lineSeparator());
    }

    private SystemLog getSystemLog(ProceedingJoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        SystemLog systemLog = methodSignature.getMethod().getAnnotation(SystemLog.class);
        return systemLog;
    }
}